package com.example.yskjproject.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewbinding.ViewBinding;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.yskjproject.R;
import com.example.yskjproject.base.BaseFragment;
import com.example.yskjproject.content.EventContent;
import com.example.yskjproject.databinding.UserInfoFragmentBinding;
import com.example.yskjproject.event.EventMessage;
import com.example.yskjproject.http.Urls;
import com.example.yskjproject.http.callback.JsonCallback;
import com.example.yskjproject.model.LzyResponse;
import com.example.yskjproject.model.UserInfoDataModel;
import com.example.yskjproject.ui.activity.AboutActivity;
import com.example.yskjproject.ui.activity.AreaAlertActivity;
import com.example.yskjproject.ui.activity.BirthdayAlertActivity;
import com.example.yskjproject.ui.activity.CustomerActivity;
import com.example.yskjproject.ui.activity.HeightAlertActivity;
import com.example.yskjproject.ui.activity.NewPhoneBindActivity;
import com.example.yskjproject.ui.activity.NickNameActivity;
import com.example.yskjproject.ui.activity.PhoneAlertActivity;
import com.example.yskjproject.ui.activity.ServiceAgreementActivity;
import com.example.yskjproject.ui.activity.SexAlertActivity;
import com.example.yskjproject.ui.activity.WechatBindActivity;
import com.example.yskjproject.utils.IntentUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * ---我的设置---
 */
public class UserInfoFragment extends BaseFragment implements View.OnClickListener {
    private UserInfoFragmentBinding mUserInfoBinding;


    @Override
    protected ViewBinding onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent) {
        mUserInfoBinding = UserInfoFragmentBinding.inflate(inflater, parent, false);
        mUserInfoBinding.llAbout.setOnClickListener(this);
        mUserInfoBinding.llArea.setOnClickListener(this);
        mUserInfoBinding.llBirthday.setOnClickListener(this);
        mUserInfoBinding.llCustomer.setOnClickListener(this);
        mUserInfoBinding.llHeight.setOnClickListener(this);
        mUserInfoBinding.llNickName.setOnClickListener(this);
        mUserInfoBinding.llWechatNum.setOnClickListener(this);
        mUserInfoBinding.llServiceAgreement.setOnClickListener(this);
        mUserInfoBinding.llSex.setOnClickListener(this);
        mUserInfoBinding.llPhoneNum.setOnClickListener(this);
        return mUserInfoBinding;
    }

    private void getUserInfoData() {
        OkGo.<LzyResponse<UserInfoDataModel>>get(Urls.USER_DATE_INFO)
                .tag(this)
                .execute(new JsonCallback<LzyResponse<UserInfoDataModel>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<UserInfoDataModel>> response) {
                        UserInfoDataModel userInfoDataModel = response.body().data;
                        Glide.with(getContext()).load(userInfoDataModel.getUser_info().getAvatar())
                                .apply(RequestOptions.circleCropTransform()).into(mUserInfoBinding.imgAvatar);
                        mUserInfoBinding.nickEd.setText(userInfoDataModel.getUser_info().getNick_name());
                        mUserInfoBinding.tvPhone.setText(userInfoDataModel.getUser_info().getMobile());
                        mUserInfoBinding.tvSex.setText(userInfoDataModel.getUser_info().getSex() == 1 ? "男" : "女");
                        mUserInfoBinding.tvAddsses.setText(userInfoDataModel.getUser_info().getAddress());
                        mUserInfoBinding.tvConstellation.setText(userInfoDataModel.getUser_info().getConstellation());
                        mUserInfoBinding.tvHeight.setText(userInfoDataModel.getUser_info().getHeight() + "");
                        if (userInfoDataModel.getUser_info().getWx_openid() != null) {
                            mUserInfoBinding.tvWxNumber.setText("已绑定");
                        } else {
                            mUserInfoBinding.tvWxNumber.setText("点击绑定微信");
                        }
                        mUserInfoBinding.tvConstellation.setText(userInfoDataModel.getUser_info().getBorthday());
                    }

                    @Override
                    public void onError(Response<LzyResponse<UserInfoDataModel>> response) {
                        super.onError(response);
                    }
                });
    }

    @Override
    protected void onData() {
        getUserInfoData();
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_nick_name:
                IntentUtils.intentActivity(getContext(), getActivity(), NickNameActivity.class, "");
                break;
            case R.id.ll_sex:
                IntentUtils.intentActivity(getContext(), getActivity(), SexAlertActivity.class, "");
                break;
            case R.id.ll_birthday:
                IntentUtils.intentActivity(getContext(), getActivity(), BirthdayAlertActivity.class, "");
                break;
            case R.id.ll_about:
                IntentUtils.intentActivity(getContext(), getActivity(), AboutActivity.class, "");
                break;
            case R.id.ll_area:
                IntentUtils.intentActivity(getContext(), getActivity(), AreaAlertActivity.class, "");
                break;
            case R.id.ll_height:
                IntentUtils.intentActivity(getContext(), getActivity(), HeightAlertActivity.class, "");
                break;
            case R.id.ll_customer:
                IntentUtils.intentActivity(getContext(), getActivity(), CustomerActivity.class, "");
                break;
            case R.id.ll_service_agreement:
                IntentUtils.intentActivity(getContext(), getActivity(), ServiceAgreementActivity.class, "");
                break;
            case R.id.ll_phone_num:
                IntentUtils.intentActivity(getContext(), getActivity(), PhoneAlertActivity.class, "");
                break;
            case R.id.ll_wechat_num:
                IntentUtils.intentActivity(getContext(), getActivity(), WechatBindActivity.class, "");
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void MessageEvent(EventMessage eventMsg) {
        switch (eventMsg.getMessage()) {
            case EventContent.update_nick_name:
                mUserInfoBinding.nickEd.setText(eventMsg.getDate());
                break;
            case EventContent.upDate_phone:
                mUserInfoBinding.tvPhone.setText(eventMsg.getDate());
                break;
            case EventContent.upDate_user:
                mUserInfoBinding.tvWxNumber.setText("已绑定");
                break;
            case EventContent.update_sex:
                String sexType = eventMsg.getDate();
                if (sexType.equals("1")) {
                    mUserInfoBinding.tvSex.setText("男");
                } else {
                    mUserInfoBinding.tvSex.setText("女");
                }
                break;
            case EventContent.update_birthday:
                mUserInfoBinding.tvConstellation.setText(eventMsg.getDate());
                break;
            case EventContent.update_height:
                mUserInfoBinding.tvHeight.setText(eventMsg.getDate());
                break;
        }
    }
}
